﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DealFile;
using BasicClass;
using System.Runtime.InteropServices;

namespace DealPLC
{
    public class ParSetTime : BaseClass
    {
        #region 静态类示例
        public static ParSetTime P_I = new ParSetTime();
        #endregion 静态类示例

        #region 定义
        public string c_PathPLC = ParPathRoot.PathRoot + "Store\\PLC\\Time.ini";

        IniFile I_I = new IniFile();

        public string RegYear { set; get; }
        
        public string RegMonth { set; get; }
        public string RegDay { set; get; }
        public string RegHour { set; get; }
        public string RegMinite { set; get; }
        public string RegSecond { set; get; }

        public bool BlAutoSetTime { set; get; }
        #endregion 定义


        #region 读写
        /// <summary>
        /// 读取时间设置
        /// </summary>
        /// <returns></returns>
        public bool ReadIni()
        {
            try
            {
                RegYear = I_I.ReadIniStr("Time", "Year", c_PathPLC);
                RegMonth = I_I.ReadIniStr("Time", "Month ", c_PathPLC);
                RegDay = I_I.ReadIniStr("Time", "Day", c_PathPLC);
                RegHour = I_I.ReadIniStr("Time", "Hour", c_PathPLC);
                RegMinite = I_I.ReadIniStr("Time", "Minite", c_PathPLC);
                RegSecond = I_I.ReadIniStr("Time", "Second", c_PathPLC);

                BlAutoSetTime = I_I.ReadIniBl("Time", "BlAutoSetTime", true, c_PathPLC);
            return true;
            }
            catch (Exception ex)
            {
                Log.L_I.WriteError(NameClass, ex);
                return false;
            }
        }


        /// <summary>
        /// 写时间设置
        /// </summary>
        /// <returns></returns>
        public bool WriteIni()
        {
            try
            {
                I_I.WriteIni("Time", "Year", RegYear, c_PathPLC);
                I_I.WriteIni("Time", "Month ", RegMonth, c_PathPLC);
                I_I.WriteIni("Time", "Day", RegDay, c_PathPLC);
                I_I.WriteIni("Time", "Hour", RegHour, c_PathPLC);
                I_I.WriteIni("Time", "Minite", RegMinite, c_PathPLC);
                I_I.WriteIni("Time", "Second", RegSecond, c_PathPLC);

                I_I.WriteIni("Time", "BlAutoSetTime", BlAutoSetTime.ToString(), c_PathPLC);
                return true;
            }
            catch (Exception ex)
            {
                Log.L_I.WriteError(NameClass, ex);
                return false;
            }
        }
        #endregion 读写

        #region 设置时间
        /// <summary>
        /// 设置系统时间
        /// </summary>
        /// <returns></returns>
        public bool SetLocalTime(out string errorinfo)
        {
            errorinfo = "";
            try
            {
                int value = 0;
                int error = 0;
                SystemTime systemTime = new SystemTime();

                //年
                if (LogicPLC.L_I.ReadReg(RegYear, out value))
                {
                    systemTime.wYear = (ushort)value;
                }
                else
                {
                    error++;
                }

                //月
                if (LogicPLC.L_I.ReadReg(RegMonth, out value))
                {
                    systemTime.wMonth = (ushort)value;
                }
                else
                {
                    error++;
                }

                //日
                if (LogicPLC.L_I.ReadReg(RegDay, out value))
                {
                    systemTime.wDay = (ushort)value;
                }
                else
                {
                    error++;
                }

                //时
                if (LogicPLC.L_I.ReadReg(RegHour, out value))
                {
                    systemTime.wHour = (ushort)value;
                }
                else
                {
                    error++;
                }

                //分
                if (LogicPLC.L_I.ReadReg(RegMinite, out value))
                {
                    systemTime.wMinute = (ushort)value;
                }
                else
                {
                    error++;
                }

                //秒
                if (LogicPLC.L_I.ReadReg(RegSecond, out value))
                {
                    systemTime.wSecond = (ushort)value;
                }
                else
                {
                    error++;
                }

                if (error > 0)
                {
                    errorinfo = "获取PLC时间失败";
                    // return false;
                }

                systemTime.wYear = 2017;
                systemTime.wMonth = 2;
                systemTime.wDay = 3;
                systemTime.wHour = 5;
                systemTime.wMinute = 5;
                systemTime.wSecond = 2;
                bool result = SetLocalTime(ref systemTime);

                if (!result)
                {
                    errorinfo = "设置系统时间失败";
                }
                return result;
            }
            catch (Exception ex)
            {
                Log.L_I.WriteError(NameClass, ex);
                return false;
            }
        }
        #endregion 设置时间

        public struct SystemTime
        {
            public ushort wYear;
            public ushort wMonth;
            public ushort wDayOfWeek;
            public ushort wDay;
            public ushort wHour;
            public ushort wMinute;
            public ushort wSecond;
            public ushort wMiliseconds;
        }

        [DllImport("Kernel32.dll")]
        public static extern bool SetLocalTime(ref SystemTime sysTime);
    }
}
